using System;

using Impact.Web.Foundation;
using Impact.Views;
using Impact.Infrastructure.Exceptions;
using Impact.Model.Objects;
using Impact.Controllers;

namespace Impact.Web.Controls
{
    public partial class DisplayCategory : Impact.Web.Foundation.DynamicPageControlBase, Impact.Views.IContentControl
    {

        /// <summary>
        /// loads the category to display into the controls that need it
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Check.Require(category, "Category is required");

            this.categoryName.Text = category.Name;

            this.rssLink.Visible = category.AllowRss;
            this.rssLink.ChannelName = category.NameKey;

            this.articleRepeater.DataSource = ArticleController.GetCategoryArticles(category.CategoryId, true);
            this.articleRepeater.DataBind();

        }

        /// <summary>
        /// Retrieves the title for the page this control is building
        /// </summary>
        protected override string PageTitle
        {
            get { return string.Format("Articles in category: {0}", Category.Name); }
        }

        private ArticleCategory category;

        /// <summary>
        /// Gets or Sets the category that will be displayed by this control
        /// </summary>
        public ArticleCategory Category
        {
            get { return category; }
            set { category = value; }
        }


        #region IContentControl Members

        /// <summary>
        /// Sets the category that this control will use as a datasource
        /// </summary>
        public object DataSource
        {
            set { category = (ArticleCategory)value; }
        }

        /// <summary>
        /// called to perform initialization before the control loads
        /// </summary>
        public void PreInit()
        {
        }

        #endregion
    }
}